package com.sirenbang.easyshop.service.impl;

import java.util.List;

import com.sirenbang.easyshop.dao.IndentDao;
import com.sirenbang.easyshop.dao.impl.IndentDaoJDBCImpl;
import com.sirenbang.easyshop.domain.Indent;
import com.sirenbang.easyshop.domain.IndentOfGood;
import com.sirenbang.easyshop.service.IndentService;
import com.sirenbang.easyshop.utils.DBUtils;
import com.sirenbang.easyshop.utils.IndentSearchHelper;
import com.sirenbang.easyshop.utils.Page;
import com.sirenbang.easyshop.utils.TransactionManager;

public class IndentServiceImpl implements IndentService {

	public void add(Indent indent) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			dao.add(indent);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}

	}

	public List<Indent> loadAll() {
		
		List<Indent> indentList=null;
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			indentList=dao.loadAll();
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indentList;
		
	}

	public Indent search(Integer indentNo) {
		
		Indent indent=null;
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			indent=dao.getIndentByNo(indentNo);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indent;
		
	}

	public void mend(Indent indent) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			dao.update(indent);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
	}

	public IndentOfGood getGoodsInfoFromIndent(Integer indentNo) {
		
		IndentOfGood indentOfGood=null;
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			indentOfGood=dao.getGoodsInfo(indentNo);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indentOfGood;
		
	}

	public void updateGoodsInfoOfIndent(IndentOfGood indentOfGood) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			dao.updateGoodsInfoOfIndent(indentOfGood);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
	}

	public Integer countIndentByHelper(IndentSearchHelper indentSearchHelper) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		Integer cnt=null;
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			cnt=dao.countByHelper(indentSearchHelper);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return cnt;
		
	}

	public Page loadScopeIndent(Page page,IndentSearchHelper indentSearchHelper) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		List<Indent> indentList=null;
		
		try{
			
			IndentDao dao=new IndentDaoJDBCImpl();
			page.setPageContent(dao.loadScopeIndent(indentSearchHelper, page.getStartIndex(), page.getEndIndex()));
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return page;
		
	}

}
